<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="filters">
        <el-form-item>
          <el-input v-model="filters.name" placeholder="商品名称"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="loadListData">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>
    <!--列表-->
    <el-table :data="listData" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="60">
      </el-table-column>
      <el-table-column prop="name" label="姓名" width="120" sortable>
      </el-table-column>
      <el-table-column prop="tel" label="电话" width="120" sortable>
      </el-table-column>
      <el-table-column prop="state" label="状态" width="100" :formatter="formatSex" sortable>
      </el-table-column>
      <el-table-column prop="address" label="地址" width="100" sortable>
      </el-table-column>
      <el-table-column prop="logo" label="图标" width="120" sortable>
      </el-table-column>
      <el-table-column label="操作" width="150">
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--分页工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination layout="total, sizes, prev, pager, next, jumper"
                     @current-change="handleCurrentChange"
                     @size-change="handleSizeChange"
                     :page-sizes="[5,10,15,20]"
                     :page-size="pageSize"
                     :total="total" style="float:right;">
      </el-pagination>
    </el-col>
    <!--新增界面-->
    <el-dialog title="title" :visible.sync="addFormVisible" :close-on-click-modal="false">
      <el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="editForm">
        <el-form-item label="商品名称" prop="name">
          <el-input v-model="addForm.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="电话号码" prop="tel">
          <el-input v-model="addForm.tel" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="addForm.state">
            <el-radio class="radio" :label="1">开启</el-radio>
            <el-radio class="radio" :label="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="addForm.address" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="图标" prop="logo">
          <el-input v-model="addForm.logo" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="addFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
      </div>
    </el-dialog>
  </section>
</template>
<script>


import {removeUser} from "../../api/api";

export default {
  data() {
    return{
      listData: [],
      sels: [],//列表选中列
      listLoading: false,
      filters: {
        name: ''
      },
      total:0,
      pageSize:10,
      page:1,
      addForm:{
        name:'',
        tel:'',
        state:1,
        address:'',
        logo:''
      },
      addFormVisible:false,
      title:'',
      addFormRules: {
        name: [
          { required: true, message: '请输入名称', trigger: 'blur' }
        ],
        tel: [
          { required: true, message: '请输入号码', trigger: 'blur' }
        ],
        address: [
          { required: true, message: '请输入地址', trigger: 'blur' }
        ],
        logo: [
          { required: true, message: '请输入图标', trigger: 'blur' }
        ],

      },
      addLoading:false,
    }
  },
  methods: {
    //修改
    editSubmit() {
      this.$refs.editForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            this.editLoading = true;
            this.$http.put("/shop",this.addForm).then(res=>{
              console.log(res);
              //关闭加载按钮
              this.addLoading=false;
              let {success,message}=res.data;
              if(success){
                this.$message({
                  message: '修改成功',
                  type: 'success'
                });
              }else {
                this.$message.error("操作失败");
              }
              //关闭模态框
              this.addFormVisible=false;
              //刷新页面
              this.loadListData();
            })
          });
        }
      });
    },
    //显示编辑界面
    handleEdit(index, row) {
      this.title='编辑';
      this.addFormVisible = true;
      this.addForm = Object.assign({}, row);
    },
    //新增
    addSubmit: function () {
      this.$refs.editForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            this.addLoading = true;
            this.$http.put("/shop",this.addForm).then(res=>{
              console.log(res);
              //关闭加载按钮
              this.addLoading=false;
              let {success,message}=res.data;
              if(success){
                this.$message({
                  message: '添加成功',
                  type: 'success'
                });
              }else {
                this.$message.error("操作失败");
              }
              //关闭模态框
              this.addFormVisible=false;
              //刷新页面
              this.loadListData();
            })
          });
        }
      });
    },
    //显示新增界面
    handleAdd: function () {
      this.addForm = Object.assign({}, {});
      this.addFormVisible = true;
      this.title='新增';

    },
    handleSizeChange(val) {
      this.pageSize=val;
      this.loadListData();
    },
    handleCurrentChange(val) {
      this.page = val;
      this.loadListData();
    },
    //批量删除
    batchRemove: function () {
      var ids = this.sels.map(item => item.id);
      this.$confirm('确认删除选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        this.$http.patch("/shop",ids).then(res=>{
          let {success,message}=res.data;
          if(success){
            this.$message({
              message: '删除成功',
              type: 'success'
            });
          }else {
            this.$message.error(message);
          }
          this.loadListData();
        })
      }).catch(() => {
        this.$message('取消删除');
      });
    },
    loadListData(){
      let param={
        currentPage:this.page,
        pageSize:this.pageSize,
        keywords: this.filters.name
      };
      this.$http.post("/shop",param).then(res=>{
        this.listData=res.data.data;
        this.total=res.data.totals;
        this.listLoading=false;
      })
    },
    handleDel(index, row) {
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        this.$http.delete("/shop/"+row.id).then(res=>{
          let {success,message}=res.data;
          if (success){
            this.$message({
              message: '删除成功',
              type: 'success'
            });
          }else {
            this.$message.error(message);
          }
          this.loadListData();
        })
      }).catch(() => {
        this.$message('取消删除');
      });
    },
    formatSex(row, column) {
      return row.state == 1 ? '启用' : row.state == 0 ? '禁用' : '未知';
    },
    selsChange: function (sels) {
      this.sels = sels;
    },
  },
  mounted() {
    this.loadListData();
  }
}

</script>

<style scoped>

</style>